import {reactive, computed, toRefs} from 'vue'
import {defineStore} from 'pinia'

export default defineStore('lottery', () => {
  const state = reactive({
    list:  [
      // name 名称 content 奖励内容 total 总共数量 unit 单位 奖励标识
      {name: '特等奖', content: '皮鞋', total: 1, unit: '只', id: 0},
    ],
    users: [
      {id: '000001', name: '阿杰', department: 'AAA'},
    ],
  });
  return {
    ...toRefs(state),
    getList() {
      return state.list;
    },
    /**
     * 添加奖品信息
     * @param obj
     * @param index
     */
    add(obj, index = undefined) {
      if (isNaN(index)) {
        state.list.push(obj);
        return;
      }
      state.list.splice(parseInt(index), 0, obj);
    },
    /**
     * 移除奖品信息
     * @param index
     */
    remove(index) {
      if (isNaN(index)) {
        return;
      }
      state.list.splice(parseInt(index), 1);
    },
    /**
     * 奖品信息排序
     * @param origin
     * @param target
     */
    sort(origin, target) {
      const item = state.list.splice(origin, 1);
      state.list.splice(target, 0, item);
    },
    /**
     * 获取所有人员信息
     * @returns {[{name: string, id: string, department: string}]}
     */
    getUsers() {
      return state.users;
    },
    /**
     * 设置人员
     * @param {{id,name,department}|{id,name,department}[]} users 人员信息
     * @param {number|undefined} index 当不为数字或小于0时 设置所有人员，否则 设置指定索引的人员信息
     */
    setUsers(users, index = undefined) {
      index = parseInt(index)
      if (isNaN(index) || index < 0) {
        state.users = users;
        return;
      }
      state.users[index] = users;
    }
  }
}, {
  persist: true,
});
